$(function() {
	"use strict";
	
	var initOrderTable = function () {

        var table = $('#order_table');

        table.DataTable({
        	"searching": false,
            "language": {
                "emptyTable": "暂无数据",
                "loadingRecords": "请稍候 - 数据加载中...",
                "info": "显示 _START_ 到 _END_ 共 _TOTAL_ 条记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "lengthMenu": "_MENU_",
                "zeroRecords": "暂无数据",
                "paginate": {
                    "previous":"上一页",
                    "next": "下一页",
                    "last": "最后一页",
                    "first": "第一页"
                }
            },
            "ordering": false,
            "lengthChange":false,
            "pageLength": 20,
            "pagingType": "full_numbers",
            "serverSide": true,
            "ajax": {
                url: context+"/app/order/list",
                type: 'POST',
                data:function(d){
                	var queryForm = $(table.data("queryForm"));
                	var x=queryForm.serializeArray();
            	    $.each(x, function(i, field){
            		 d[field.name] = field.value;
            	    });
                },
                dataSrc: 'orderList'
            },
            "columns": [
                { "data": "orderNo" },
                { "data": "nickName",
                  "render":function( data, type, full, meta ){
                	  return decodeURI(data);
                  }	
                },
                { "data": "goodsName" },
                { "data": "createTime",
                  "render":formatTime	
                },
                { "data": "tradeNum" },
                { "data": "orderPrice" },
                { "data": "detailInfo" ,
                  "render":function( data, type, full, meta ){
                	  return full.provinceName+full.cityName+full.countyName+data;
                  }
                },
                { "data": "userName" },
                { "data": "telNumber" },
                { "data": "status",
                  "render":function( data, type, full, meta ){
                	  if(data==0){
                		  return '待支付';
                	  }
                	  if(data==1){
                		  return '待发货';
                	  }
                	  if(data==2){
                		  return '已发货';
                	  }
                	  if(data==3){
                		  return '已撤销';
                	  }
                	  if(data==4){
                		  return '已关闭';
                	  }
                  }	
                },
                { "data": "orderNo",
                  "width":200,
                  "render": function ( data, type, full, meta ) {
                	var str = '';
                	if(full.status==1){
                		str = str+'<a href="javascript:;" class="btn btn-default btn-sm send" data-order-no='+data+'><i class="fa fa-send"></i> 发货 </a>&nbsp;';
                	}
                	if(full.status==0||full.status==1){
                		str = str+'<a href="javascript:;" class="btn btn-danger btn-sm cancel" data-order-no="'+data+'"><i class="fa fa-remove"></i> 撤销 </a>&nbsp;';
                	}
                    return str;
                  }
                }
            ]
        });
    }
	
	var initQueryBtn = function(){
		$(".query").on('click',function(){
			var tableId = $(this).data("table");
			var table = $(tableId).DataTable();
			if(table){
				table.draw(false);
			}
		});
	}
	
	var initOperBtn = function(){
		$('#order_table').delegate('.send','click',function(){
			var dialog = $("#orderSendDialog");
			var form = $("#orderSendForm",dialog);
			
			var orderNo = $(this).data('orderNo');
			$("input[name='orderNos']",form).val(orderNo);
			
			dialog.modal();
		});
		
		$(".ok","#orderSendDialog").on('click',function(){
			var dialog = $("#orderSendDialog");
			var form = $("#orderSendForm",dialog);
			
			App.blockUI({
                target: '#orderSendDialogContent',
                boxed: true,
                message: '发货处理中...'
            });
			
			$.ajax({
	            url:context+'/app/order/send',
	            type:'POST',
	            data:form.serialize(),
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#orderSendDialogContent');
	                if(data.success){
	                	toastr["info"]("发货成功", "提示");
	                	$('#order_table').DataTable().draw(false);
	                	dialog.modal('hide');
	                }else{
	                	toastr["error"]("发货异常", "提示");
	                }
	                
	            }
	        });
		});
		
		$('#order_table').delegate('.cancel','click',function(){
			var orderNo = $(this).data("orderNo");
			bootbox.dialog({
				size:'small',
                message: "确定撤销订单？",
                title: "提示",
                buttons: {
                  success: {
                    label: "确定",
                    className: "green",
                    callback: function() {
                    	App.blockUI({
                            boxed: true,
                            message: '数据处理中...'
                        });
                    	$.ajax({
            	            url:context+'/app/order/cancel',
            	            type:'POST',
            	            data:{orderNos:orderNo},
            	            dataType:'json',
            	            success:function(data,textStatus,jqXHR){
            	            	App.unblockUI();
            	                if(data.success){
            	                	toastr["info"]("订单撤销成功", "提示");
            	                	$('#order_table').DataTable().draw(false);
            	                }else{
            	                	toastr["error"]("订单撤销异常", "提示");
            	                }
            	                
            	            }
            	        });
                    }
                  },
                  main: {
                    label: "关闭",
                    className: "default",
                    callback: function() {
                    }
                  }
                }
            });
		});
	}
	
	var initExportKuaidi100 = function(){
		$(".exportKuaidi100").on('click',function(){

			var tempForm = document.createElement("form");
			tempForm.id = "tempForm1";
			tempForm.method = "post";
			tempForm.action = context+"/app/order/download/excel/kuaidi100";
			tempForm.target = '_self';
			
			var queryForm = $($('#order_table').data("queryForm"));
			
        	var x=queryForm.serializeArray();
    	    $.each(x, function(i, field){
    	    	var hideInput = document.createElement("input");
    	    	hideInput.type = "hidden";
    	    	hideInput.name = field.name;
    	    	hideInput.value = field.value;
    	    	tempForm.appendChild(hideInput);
    	    });
			
			document.body.appendChild(tempForm);
			tempForm.submit();

			document.body.removeChild(tempForm);

		});
	}
	
	var initExportZhongtong = function(){
		$(".exportZhongtong").on('click',function(){
			
			var tempForm = document.createElement("form");
			tempForm.id = "tempForm1";
			tempForm.method = "post";
			tempForm.action = context+"/app/order/download/excel/zhongtong";
			tempForm.target = '_self';
			
			var queryForm = $($('#order_table').data("queryForm"));
			
        	var x=queryForm.serializeArray();
    	    $.each(x, function(i, field){
    	    	var hideInput = document.createElement("input");
    	    	hideInput.type = "hidden";
    	    	hideInput.name = field.name;
    	    	hideInput.value = field.value;
    	    	tempForm.appendChild(hideInput);
    	    });
			
			document.body.appendChild(tempForm);
			tempForm.submit();

			document.body.removeChild(tempForm);

		});
	}
	
	var initImportZhongtong = function(){
		$(".importZhongtong").on('click',function(){
		
			$("#importZhongtongDialog").modal();
			
		});
		
		$(".ok","#importZhongtongDialog").on('click',function(){
			App.blockUI({
                target: '#importZhongtongDialogContent',
                boxed: true,
                message: '数据导入...'
            });
			$("#importZhongtongForm").ajaxSubmit({
				url:context+'/app/order/import/zhongtong',
				type:'POST',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#importZhongtongDialogContent');
	                if(data.success){
	                	toastr["info"]("中通数据导入成功", "提示");
	                }else{
	                	toastr["error"]("中通数据导入异常", "提示");
	                }
	                
	            }
			});
		});
	}

	initOrderTable();
	initQueryBtn();
	initOperBtn();
	initExportKuaidi100();
	initExportZhongtong();
	initImportZhongtong();
	
	if (jQuery().datepicker) {
        $('.date-picker').datepicker({
            rtl: App.isRTL(),
            orientation: "left",
            autoclose: true
        });
    }
	
});